package com.woniuxy.yygh.cmn.controller;

import com.woniuxy.yygh.cmn.service.DictService;
import com.woniuxy.yygh.common.result.Result;
import com.woniuxy.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <P> @Author: ideaAdmin
 * <P>  Class
 * <P> des: 数据字典管理
 * <P> Date: 2022/2/28:11:06
 * <P> Description:
 */
@Api("数据字典管理")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {
    @Autowired
    DictService dictService;

    @ApiOperation(value = "根据 数据字典ditcode 查询下级子节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){
        List<Dict> dicts = dictService.findByDictCode(dictCode);

        return Result.ok(dicts);
    }


    @ApiOperation(value = "根据数据id 查询子节点列表")
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable Long id){
        List<Dict> dicts = dictService.findChildData(id);
        return Result.ok(dicts);
    }

    /**
        * TODO:导出数据字典接口
    **/
    @GetMapping("/exportData")
    public void exportDict(HttpServletResponse response){
        dictService.exportDistData(response);
       // return Result.ok();
    }

    /**
     * TODO:导入数据字典接口
     **/
    @PostMapping("/importData")
    public Result importDict(MultipartFile file){
        dictService.importData(file);
        return  Result.ok();
    }



    /**
        * TODO: 根据 dictcode 和 value 查询
     *          根据当前所属的字典范围 根据value值来获取对应值
    **/
    @GetMapping("/getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value){

       String dictName =  dictService.getDictName(dictCode,value);
       return  dictName;
    }

    /**
        * TODO: 根据字段value查询
    **/
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable String value ){
        String dictName =  dictService.getDictName("",value);
        return  dictName;
    }

    @GetMapping("/test")
    public Result test(){
       return dictService.findAll();
    }

}
